package com.eedi.framework.info.dal.mapper;

import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.info.controller.org.vo.OrgInfoCreateReq;
import com.eedi.framework.info.controller.org.vo.OrgInfoPageReq;
import com.eedi.framework.info.controller.org.vo.OrgInfoResp;
import com.eedi.framework.info.controller.org.vo.OrgInfoUpdateReq;
import com.eedi.framework.info.convert.OrgInfoConvert;
import com.eedi.framework.info.dal.dataobject.OrgInfoDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;

@Mapper
public interface OrgInfoMapper extends BaseMapperX<OrgInfoDO> {

    default OrgInfoResp selectByOrgUscCode(String orgUscCode){
        return OrgInfoConvert.INSTANCE.convert(selectOne(OrgInfoDO::getOrgUscCode,orgUscCode));
    }

    default OrgInfoResp create(OrgInfoCreateReq createReq){
        OrgInfoDO item = BeanUtils.toBean(createReq, OrgInfoDO.class);
        insert(item);
        return OrgInfoConvert.INSTANCE.convert(item);
    }

    default OrgInfoResp selectByOrgInfoId(String orgInfoId){
        return OrgInfoConvert.INSTANCE.convert(selectOne(OrgInfoDO::getOrgInfoId,orgInfoId));
    }

    default int update(OrgInfoUpdateReq updateReqVO){
        OrgInfoDO item = BeanUtils.toBean(updateReqVO, OrgInfoDO.class);
        return updateById(item);
    }

    default int updateStatusByOrgInfoId(String orgInfoId, CommonStatusEnum commonStatusEnum){
        OrgInfoDO item = new OrgInfoDO();
        item.setOrgInfoId(orgInfoId);
        item.setOrgInfoStatus(commonStatusEnum);
        return updateById(item);
    }


    default PageResult<OrgInfoResp> page(OrgInfoPageReq req) {
        PageResult<OrgInfoDO> orgInfoDoPageResult = selectPage(req, new LambdaQueryWrapperX<OrgInfoDO>()
                .likeIfPresent(OrgInfoDO::getOrgName, req.getOrgName())
                .likeIfPresent(OrgInfoDO::getOrgUscCode, req.getOrgUscCode())
                .eqIfPresent(OrgInfoDO::getOrgInfoStatus,req.getOrgInfoStatus())
                .orderByDesc(OrgInfoDO::getId));
        return orgInfoDoPageResult.convertPage(OrgInfoConvert.INSTANCE::convert);
    }

    default List<OrgInfoResp> selectByOrgInfoIds(Collection<String> orgInfoIds){
        List<OrgInfoDO> orgInfos = selectBatchIds(orgInfoIds);
        return OrgInfoConvert.INSTANCE.convert(orgInfos);
    }

    @Select("select (org_info_id || '#' || sys_tenant_id) as tmp FROM org_info WHERE org_info_status = 1 and deleted = 0")
    List<String> selectAllOrgInfos();

}
